package com.ycz.springcloud.config;/*
 @author ycz
 @date 2021-08-22-2:29  
*/

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyConfig implements WebMvcConfigurer {
    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**") //拦截所有请求
                //                    登录页面       发出登录请求
                .excludePathPatterns("/index/**","/","/consumer/sysuser/login.do",
                        //跳转到登录页面
                        "/page/index.do",
                        "/resources/**","/upload/**");//不拦截静态资源
    }

    /**
     * 跳转到 /index页面
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 跳转方式为：转发; 第一个参数为路径, 第二个参数为视图路径名称
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
    }
    /**
     * 国际化
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

//    @Bean
//    public HttpMessageConverters fastJsonHttpMessageConverters() {
//        // 1. 需要定义一个Convert转换消息的对象
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        // 2.添加fastjson的配置信息，比如是否要格式化返回的json数据
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.PrettyFormat,
//                SerializerFeature.WriteNullStringAsEmpty,
//                SerializerFeature.WriteNullListAsEmpty,
//                SerializerFeature.MapSortField,
//                // 循环引用
//                SerializerFeature.DisableCircularReferenceDetect);
//        // 3.在convert中添加配置信息
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        HttpMessageConverter<?> converter = fastConverter;
//        return new HttpMessageConverters(converter);
//    }

}
